package com.ysd.twocrm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysd.twocrm.entity.Student;
import com.ysd.twocrm.entity.Weekbook;
import com.ysd.twocrm.mapper.StudentMapper;
import com.ysd.twocrm.mapper.WeekbookMapper;
import com.ysd.twocrm.service.WeekBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/weekbook")
public class WeekbookController {
    @Autowired
    private WeekbookMapper weekbookMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private WeekBookService weekBookService;
    /*
     * @Description 根据用户id来查询当前用户下的全部周报
     * @param userId
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/17 14:51
     */
    @GetMapping
    public Object findAllWeekBookByUserId(Integer userId) {

        QueryWrapper<Weekbook> queryWrapper = new QueryWrapper<Weekbook>();
        queryWrapper.eq("userId", userId);
        return weekbookMapper.selectList(queryWrapper);
    }
    /*
     * @Description 根据主键来查询当前的周报
     * @param wid
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/17 14:52
     */
    @GetMapping("/1")
    public Object findAllWeekBookByWid(Integer wid){

        QueryWrapper<Weekbook> queryWrapper = new QueryWrapper<Weekbook>();
        queryWrapper.eq("wid", wid);
        return weekbookMapper.selectOne(queryWrapper);
    }
    /*
     * @Description 多条件查询周报
     * 根据用户id来查询，首先先获取到当前辅导员拥有的班级下的全部学生对应的用户id
     * 然后将他放到集合里面如何传给我们的service层里的业务
     * 还有根据开始时间、结束时间、姓名、状态
     * @param wk
     * @param page
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/17 16:32
     */
    @GetMapping("/2")
    public Object findAllLayuiByUserId(Weekbook wk,Integer page,String begintime,String endtime) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
          if(begintime!=null&&begintime!=""){
               wk.setWbegintime(sdf.parse(begintime));
          }
        if(endtime!=null&&endtime!=""){
            wk.setWbegintime(sdf.parse(endtime));
        }
        List<Student> allStudentExit1ByUserId = studentMapper.findAllStudentExit1ByUserId(wk.getUserId());
        List<Integer> userIdList=new ArrayList<Integer>();
        for(int i=0;i<allStudentExit1ByUserId.size();i++){
            userIdList.add(allStudentExit1ByUserId.get(i).getExit1());
        }

        return weekBookService.LAYUI_UTILS(wk,page,userIdList);
    }


    /*
     * @Description
     *  1、根据wid来修改当前状态，用于发布与未发布
     * 2、也可根据wid来修改当前的全部信息
     * @param wb
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/17 14:51
     */
    @PutMapping
    public Object exitStatusWeekBookByWId(Weekbook wb) {
        wb.setWsubmittime(new Date());
        int i = weekbookMapper.updateById(wb);
        if (i > 0) {
            return "success";
        }

        return "false";
    }



}


